import requests
from pyquery import PyQuery as pq
import re
import json


URL = 'https://www.saxstar.club/pic/xinlang_copy.html'

headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
            'Host': 'www.saxstar.club'
        }

class Nogizaka():

    def __init__(self):

        self.url = URL
        self.headers = headers

    def get_request(self):
        
        response = requests.get(url=self.url, headers=self.headers).text
        return response

    def analysis(self, html):
        
        doc = pq(html)
        node_list = doc('.member')
        text_list = []
        for item in node_list.items():
            text_list.append((item.attr('id')))
        return text_list


    def split_url(self, *name):

        base_url = 'https://www.saxstar.club/nogizaka/getImageUrl?name='
        url_api = []
        for name_list in name:
            for item in name_list:
                url_api.append(base_url + item + '.txt')
        return url_api


    def get_real_url(self, url):

        response = requests.get(url, headers=self.headers).text
        # doc = pq(response)
        # return doc('p').text()
        return response


    def split_img(self, txt):

        for api in range(0, len(txt)):
            # print(txt[api])
            img = self.get_real_url(txt[api])
            
            img_list = re.split('{|}|\[|\]', img)
            # print(img_list)
            # print(img_list[3])
            img_list_split = img_list[3].split(',')
            array_list = []
            array_list.append(img_list_split)
            return array_list


    def get_json(self, txt):

        for item in txt:
            content = self.get_real_url(item)
            content = re.split('\[|\]', content)
            content_str = ''.join(content)
            # print(content_str)
            # print(type(content_str))
            img_list = json.dumps(content_str)
            # print(img_list)
            # print(type(img_list))
            return img_list

    def storage_img(self, *img_json):
        
        filename = ''

        for img in img_json:
            print(img)
            # try:
            #     with open('/Users/mac/Pictures/Nogizaka/weibo' + filename, 'wb') as f_obj:
            #         f_obj.write()

    def main(self):
        
        html = self.get_request()
        name = self.analysis(html)
        # print(name)
        txt = self.split_url(name)
        # print(txt)
        # img_str = self.split_img(txt)
        # print(img_str)
        res = self.get_json(txt)
        # print(type(res))
        # print(res)
        

        # img_url = res.loads(res)
        # print(img_url)




if __name__ == "__main__":
    
    img = Nogizaka()
    img.main()
